package org.example.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * socket 网络IO server端代码
 * {@link SocketClient} 这里是客户端代码
 */
public class SocketIOProperties {
    private static final int RECEIVE_BUFFER = 10;
    private static final int SO_TIMEOUT = 0;
    private static final boolean REUSE_ADDR = false;
    /**
     * 配置最大的等待连接个数。也就是完成了三次握手的连接最多存在的个数-1。
     */
    private static final int BACK_LOG = 2;
    /**
     * 心跳，检测是否断开连接
     */
    private static final boolean CLI_KEEPALIVE = false;
    /**
     * 是否先发送一个数字进行试探
     */
    private static final boolean CLI_OOB = false;
    /**
     *
     */
    private static final int CLI_REC_BUF = 20;
    /**
     * 是否重用地址
     */
    private static final boolean CLI_REUSE_ADDR = false;
    /**
     * 发送缓冲区大小
     */
    private static final int CLI_SEND_BUF = 20;
    /**
     *
     */
    private static final boolean CLI_LINGER = true;
    /**
     *
     */
    private static final int CLI_LINGER_N = 0;
    /**
     * 客户端超时时间
     */
    private static final int CLI_TIMEOUT = 0;
    /**
     * 是否开启节点延迟
     */
    private static final boolean CLI_NO_DELAY = false;

    public static void main(String[] args) throws IOException {
        ServerSocket server = null;
        try {
            server = new ServerSocket();
            server.bind(new InetSocketAddress(9090), BACK_LOG);
            server.setReceiveBufferSize(RECEIVE_BUFFER);
            server.setReuseAddress(REUSE_ADDR);
            server.setSoTimeout(SO_TIMEOUT);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("server up use 9090!");
        try{
            while(true){
                System.in.read(); // 分水岭

                final Socket client = server.accept();
                System.out.println("Client port:" + client.getPort());

                client.setKeepAlive(CLI_KEEPALIVE);
                client.setOOBInline(CLI_OOB);
                client.setReceiveBufferSize(CLI_REC_BUF);
                client.setReuseAddress(CLI_REUSE_ADDR);
                client.setSendBufferSize(CLI_SEND_BUF);
                client.setSoLinger(CLI_LINGER,CLI_LINGER_N);
                client.setSoTimeout(CLI_TIMEOUT);
                client.setTcpNoDelay(CLI_NO_DELAY);

                // client.read
                new Thread(()->{
                    try {
                        InputStream in = client.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                        char[] data = new char[1024];
                        while(true){

                            int num = reader.read(data);

                            if (num>0){
                                System.out.println("client read some data is:" + num + " val :" + new String(data,0,num));
                            }else if (num == 0){
                                System.out.println("client readed nothing!");
                                continue;
                            }else {
                                System.out.println("client readed -1....");
                                System.in.read();
                                client.close();
                                break;
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            server.close();
        }
    }
}
